﻿@using System.Text.RegularExpressions
@model StackExchange.Opserver.Data.SQL.SQLInstance
@{
    Layout = null;
    var files = Model.DatabaseFiles.SafeData(true).OrderBy(f => f.PhysicalName);
    var volumes = files.GroupBy(p => p.VolumeMountPoint);
}
@functions
{
    static readonly Regex CommonPathMatcher = new Regex(@"C:\\Program Files\\Microsoft SQL Server\\MSSQL\d+.MSSQLSERVER\\MSSQL\\DATA", RegexOptions.Compiled);
}
@helper ShortenPath(string path)
{
    var match = CommonPathMatcher.Match(path);
    if (match.Success)
    {
        var shortVersion = CommonPathMatcher.Replace(path, @"C:\Program...MSSQLSERVER\MSSQL\DATA");
        <span title="@path">@shortVersion</span>
    }
    else
    {
        @path
    }
}
@if (files.Any())
{
    <div class="summary-dashboard">
        <h3>
            <a href="/sql/instance?node=@Model.Name">@Model.Name</a><span class="note">:</span> Database Files
            <a href="/sql/db-files?node=@Model.Name" class="top-right-nav">View Detail</a>
        </h3>
        <div>
            <div class="summary-header">
                @foreach (var v in volumes)
                {
                    <div class="section-wrap half-wrap volume">
                        <div class="volume-label" style="float: left;">@v.Key</div>
                        <div class="volume-usage" style="clear: none; float: right;">
                            <div>Read: <span class="important">@((v.Sum(f => f.StallReadMs) / (decimal)v.Sum(f => f.NumReads)).ToString("F"))</span><span class="note"> ms</span></div>
                            <div>Write: <span class="important">@((v.Sum(f => f.StallWriteMs) / (decimal)v.Sum(f => f.NumWrites)).ToString("F"))</span><span class="note"> ms</span></div>
                        </div>
                    </div>
                }
            </div>
            <table id="db-files-table">
                <thead>
                    <tr>
                        <th>Database</th>
                        <th>Type</th>
                        <th>Path</th>
                        <th>Size</th>
                        <th>Avg Read</th>
                        <th>Avg Write</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var f in files)
                    {
                        <tr>
                            <td>@f.DatabaseName</td>
                            <td>@f.FileType.GetDescription()</td>
                            <td title="Virtual Name: @f.FileName">@ShortenPath(f.PhysicalName)</td>
                            <td data-val="@f.FileSizeBytes">@f.FileSizeBytes.ToHumanReadableSize()</td>
                            <td title="Reads: @f.NumReads.ToComma()
Stalled: @f.StallReadMs.ToComma()">@f.AvgReadStallMs</td>
                            <td title="Writes: @f.NumWrites.ToComma()
Stalled: @f.StallWriteMs.ToComma()">@f.AvgWriteStallMs</td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    </div>
    <script>
        $(function () {
            $('#db-files-table').tablesorter({
                headers: {
                    3: { sorter: 'dataVal', sortInitialOrder: 'desc' },
                    4: { sortInitialOrder: 'desc' },
                    5: { sortInitialOrder: 'desc' }
                }
            });
        });
    </script>
}
else
{
    <div class="no-content">No Database Files</div>
}
